<?php
namespace Shopmobile\Model;
use Think\Model;
class FriendshipsModel extends Model{
	public $guest_mixednick; //朋友的昵称
	public $home_mixednick; //自己的昵称
	public $guest_id; //朋友的ID
	public $home_id; //自己的ID
	public $activity_id; //平台的折扣ID
	public $promo_id; //本地折扣ID
	public $promo_cat_id; //本地活动ID
	public $invite_order; //邀请顺序
	public $sid;
	public function addFriendShip(){
		$data["home_mixednick"] = $this->home_mixednick;
		$data["guest_mixednick"] = $this->guest_mixednick;
		$data["home_id"] = $this->home_id;
		$data["guest_id"] = $this->guest_id;
		$data["invite_time"] = date("Y-m-d H:i:s");
		$data["sid"] = $this->sid;
		$data["invite_order"] = $this->invite_order;
		return $this->add($data);
	}
	public function getFriendShip(){
		$data["sid"] = $this->sid;
		$data["home_id"] = $this->home_id;
		$data["guest_id"] = $this->guest_id;
		return $this->where($data)->getField("`id`,`invite_time`");
	}
	public function invite(){
			$USR = D("UserView");
			if(!$this->home_id && $this->home_mixednick){
				$USR->sid = $this->sid;
				$USR->mixednick = $this->home_mixednick;
				$USR->getUser();
				$this->home_id = $USR->user_info["shop_uid"];
			}
			$where["guest_id"]=$this->guest_id;
			$where["home_id"]=$this->home_id;
			$where["sid"]=$this->sid;
			$ship = $this->cache(!APP_DEBUG)->where($where)->getField("`id`,`invite_time`");

			if(!$ship){
				$UIF = M("ShopUsersInfo");
				$uif["shop_uid"]=$this->guest_id;
				$uif["sid"]=$this->sid;
				$re = $UIF->where($uif)->setInc("friends");
				//朋友数量增加写入到数据库
				
				//准备存入朋友关系。朋友链的主客关系刚好相反
				// $USR->myfields="friends";
				// $this->invite_order = $USR->getUserField();
				$this->addFriendShip(); //朋友关系增加
			}
			return $this->invite_order;
	}
	public function get_code_img(){
		$USR = M("ShopUsersInfo");
		$udata["sid"] = $this->sid;
		$udata["shop_uid"] = $this->home_id;
		$friend = current($USR->where($udata)->getField("id,qrcode_url,friend_link,short_url"));
echo $USR->_sql();
		if($friend){
			if($friend["qrcode_url"]){
				$resp = new \stdClass();
				$resp->modules->qrcode_d_o->qrcode_url = $friend["qrcode_url"];
				$resp->modules->qrcode_d_o->url = $friend["friend_link"];
				$resp->modules->qrcode_d_o->short_url = $friend["short_url"];
				$resp->suc=true;
			}else{
				echo "loading codeimg";
				$resp=$this->getQr();
				print_r($resp);
				die();
				$wh["shop_uid"]=$this->home_id;
				$wh["sid"]=$this->sid;
				
				$sa["qrcode_url"] = (string)$resp->modules->qrcode_d_o->qrcode_url;
				$sa["short_url"] = (string)$resp->modules->qrcode_d_o->short_url;
				$sa["eps_url"] = (string)$resp->modules->qrcode_d_o->eps_url;
				$sa["channel_id"] = (string)$resp->modules->qrcode_d_o->channel_id;
				$sa["channel_name"] = (string)$resp->modules->qrcode_d_o->channel_name;
				
				$USR->where($wh)->save($sa);
			}
		}else{
			$resp=$this->getQr();
			$sa["shop_uid"]=$this->home_id;
			$sa["sid"]=$this->sid;
			$sa["qrcode_url"] = (string)$resp->modules->qrcode_d_o->qrcode_url;
			$sa["short_url"] = (string)$resp->modules->qrcode_d_o->short_url;
			$sa["eps_url"] = (string)$resp->modules->qrcode_d_o->eps_url;
			$sa["channel_id"] = (string)$resp->modules->qrcode_d_o->channel_id;
			$sa["channel_name"] = (string)$resp->modules->qrcode_d_o->channel_name;
			$sa["friend_link"]=C("URL")."/?friend=".urlencode($this->home_mixednick)."&sid=".$this->sid."&eid=".$this->eid;
			$USR->add($sa);
		}
		$resp = json_decode(json_encode($resp),true);
		return ($resp);
	}
	private function getQr(){
		Vendor("taobaosdk.top.TopClient");
		Vendor("taobaosdk.lotusphp_runtime.Logger.Logger");
		Vendor("taobaosdk.top.RequestCheckUtil");
		Vendor("taobaosdk.top.request.MaQrcodeCommonCreateRequest");
		Vendor("taobaosdk.top.request.WirelessXcodeCreateRequest");
		$c = new \TopClient;
		$c->appkey = C("SM_KEY");//用后台的key
		$c->secretKey = C("SM_SECRET"); 
		$req = new \MaQrcodeCommonCreateRequest;
		$req->setType("url");
		$req->setContent(C("URL")."/?friend=".urlencode($this->home_mixednick)."&sid=".$this->sid."&eid=".$this->eid);
		$req->setName("淘小店的无线店铺首页");
		$req->setStyle("EF4F2B");
		$req->setSize("300");
		$req->setNeedEps("false");
		$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");

		$SHOP = D("ShopCfg");
		$sessionKey = $SHOP->cache(!APP_DEBUG)->where("`sid`=".$this->sid)->getField("sessionkey");

		$resp = $c->execute($req, $sessionKey);
		return $resp;
	}
	
}